<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/23
 * Time: 11:43
 */

namespace App\Game;

use App\Models\Def\MainGamePlat;
use App\Models\Inf\Player;
use App\Models\Log\PlayerBetFlow;
use App\Models\OddsHandicap;
use Carbon\Carbon;
use App\Game\Traits\SgTrait;
use Illuminate\Http\Request;
use App\Game\Traits\IgTrait;

class ig extends Game
{
    use IgTrait;

    private $platName = 'ig';

    public function __construct()
    {
        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/json",
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/24 14:16
     * Administrator
     * curlCreateMember
     * 创建用户
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        //得请求登录接口，才会创建
        $data = [
            'username' => $accountUserName,
            'password' => md5($password),
            'currency' => 'CNY',
            'nickname' => $accountUserName,
            'language' => 'CN',
            'gameType' => 'GFC',
            'gfcTray' => 'A',
            'gfcType' => 'PC',
            'gfcPage' => '2',
            'line' => 0,
            'homeUrl' => $this->backUrl,
        ];

        $output = $this->igrequest('LOGIN', $this->loginUrl, $data);

        if (array_key_exists('errorCode', $output)) {
            if ($output['errorCode'] == 0) {
                return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：' . $output['errorMessage']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 14:15
     * Administrator
     * curlGetBalance
     * 查询余额
     * @param $accountUserName
     * @return array
     */
    public function curlGetBalance($accountUserName,$request)
    {
        $gamePlat = MainGamePlat::where('main_game_plat_code', str_replace('App\\Game\\', '', $request->mainGamePlat))->first();
        $player = Player::where('username', $accountUserName)->where('main_game_plats_id', $gamePlat->id)->first();
        $data = [
            'username' => $accountUserName,
            'password' => md5($player->password),
        ];

        $output = $this->igrequest('GET_BALANCE', $this->moneyUrl, $data);

        if (isset($output['errorCode'])){
            if ($output['errorCode'] == 0) {
                return ['success' => true, 'data' => ['balance' => $output['params']['balance']], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '查询余额失败，错误原因：' . $output['errorMessage']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }


    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid,$mainGamePlatId)
    {
        $player = Player::where('username', $accountUserName)->where('main_game_plats_id', $mainGamePlatId)->first();
        //得请求登录接口，才会创建
        $data = [
            'username' => $accountUserName,
            'password' => md5($player->password),
            'ref' => $transferid,
            'desc' => '转入游戏',
            'amount' => $price.'.00',
        ];

        $output = $this->igrequest('DEPOSIT', $this->moneyUrl, $data);

        if (isset($output['errorCode'])) {
            if ($output['errorCode'] == 0) {
                sleep(1);
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败，错误原因：' . $output['errorMessage']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid,$mainGamePlatId)
    {
        $player = Player::where('username', $accountUserName)->where('main_game_plats_id', $mainGamePlatId)->first();
        //得请求登录接口，才会创建
        $data = [
            'username' => $accountUserName,
            'password' => md5($player->password),
            'ref' => $transferid,
            'desc' => '转出游戏',
            'amount' => $price.'.00',
        ];

        $output = $this->igrequest('WITHDRAW', $this->moneyUrl, $data);

        if (isset($output['errorCode'])) {
            if ($output['errorCode'] == 0) {
                sleep(1);
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败，错误原因：' . $output['errorMessage']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $json = [
            'json' => [
                'MerchantCode' => $this->merchantCode,
                'PlayerId' => $request->accountUserName,
                'ProductWallet' => $this->productWallet,
                'TransactionId' => $request->transferid,

            ]
        ];

        $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_CHECK_TRANSFER, [], [], $json);
        $output = json_decode($output, true);

        if (array_key_exists('Code', $output)) {
            if ($output['Code'] == 0 && $output['Message'] == 'Successful.') {
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
            }elseif($output['Code'] == 541){
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败' . $output['Message']];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '检查转账失败，错误原因：' . $output['Message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //踢线
    public function curlKick($accountUserName)
    {
        return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
    }

    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinGame
     * 进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinGame(Request $request)
    {
        //得请求登录接口，才会创建
        $data = [
            'username' => $request->accountUserName,
            'password' => md5($request->password),
            'currency' => 'CNY',
            'nickname' => $request->accountUserName,
            'language' => 'CN',
            'gameType' => 'GFC',
            'gfcTray' => 'A',
            'gfcType' => 'PC',
            'line' => 0,
            'homeUrl' => $this->backUrl,
        ];

        $output = $this->igrequest('LOGIN', $this->loginUrl, $data);

        if (isset($output['errorCode'])) {
            if ($output['errorCode'] == 0) {
                return ['success' => true, 'data' => ['url' => $output['params']['link']], 'message' => '成功'];
            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['errorMessage']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }


    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinMobileGame
     * 手机端进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame(Request $request)
    {
        //得请求登录接口，才会创建
        $data = [
            'username' => $request->accountUserName,
            'password' => md5($request->password),
            'currency' => 'CNY',
            'nickname' => $request->accountUserName,
            'language' => 'CN',
            'gameType' => 'GFC',
            'gfcTray' => 'A',
            'gfcType' => 'MP',
            'line' => 0,
            'homeUrl' => $this->backUrl,
        ];

        $output = $this->igrequest('LOGIN', $this->loginUrl, $data);

        if (isset($output['errorCode'])) {
            if ($output['errorCode'] == 0) {
                return ['success' => true, 'data' => ['url' => $output['params']['link']], 'message' => '成功'];
            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['errorMessage']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
//        file_put_contents('/home/gameapi/storage/logs/test.log', '3333...'.PHP_EOL, FILE_APPEND);
        $data = [];
        //得请求登录接口，才会创建
        $beginId = '0';
        $playerBetFlow = PlayerBetFlow::where('main_game_plat_code', $this->platName)
            ->whereNotNull('pull_at')
            ->orderBy('pull_at', 'desc')
            ->first();
        if ($playerBetFlow) {
            $beginId = $playerBetFlow->bet_detail;
        }
        $datas = [
            'count' => '3000',
            'beginId' => $beginId,
        ];

        $output = $this->igrequest('GET_GFC_RECORD_BY_SEQUENCENO', $this->loginUrl, $datas);

        if (isset($output['errorCode']) && $output['errorCode'] == 0 && $output['params']['recordList']) {
            $data = $output['params']['recordList'];
        }

//        return [];
        return $data;
    }
}